package com.etone.smartAudit.service;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.etone.smartAudit.domain.PLogVersion;
import com.etone.smartAudit.mapper.PLogVersionMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author jiayen
 * @since 2021-12-10 09:25:13
 */
@Slf4j
@Service
public class PLogVersionService extends ServiceImpl<PLogVersionMapper, PLogVersion> {

    //    用户#200001-516665；
//    组织#600001-748922；  步阶为500
    public static final int ORG_TYPE = 0;
    public static final int EMPLOYEE_TYPE = 1;

    @Value("${uds.startLog:0}")
    private Integer startLog;

    /***
     * 获取最后一个组织同步版本
     * @return
     */
    public synchronized PLogVersion getOrgnizationLog() {
        LambdaQueryWrapper<PLogVersion> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PLogVersion::getType, 0)
                .eq(PLogVersion::getIsHistoryVersion, 1);
        PLogVersion version = this.getOne(queryWrapper);
        if (version == null) {
//            return initPLog(0, 1, "600001", 748922);
            return initPLog(0, 1, "600001", 748922);
        }
        if (Integer.valueOf(version.getLogId()) > (version.getMaxLogId() == null ? 748922 : version.getMaxLogId()) + 501 + 501) {
            log.error("===========组织同步完成===================");
            return null;
        }
        return getLogVersion(queryWrapper);
    }

    /***
     * 获取版本号
     * @param queryWrapper
     * @return
     */
    private PLogVersion getLogVersion(LambdaQueryWrapper<PLogVersion> queryWrapper) {
        PLogVersion version = this.getOne(queryWrapper);
        //更新 先深拷贝
        PLogVersion copyVersion = new PLogVersion();
        BeanUtils.copyProperties(version, copyVersion);
        copyVersion.setLogId(Integer.valueOf(version.getLogId()) + 500 + "");
        copyVersion.setVersion((version.getVersion() == null ? 0 : version.getVersion()) + 1);
        //执行更新
        queryWrapper.eq(PLogVersion::getVersion, version.getVersion());
        queryWrapper.eq(PLogVersion::getId, version.getId());
        boolean flag = this.update(copyVersion, queryWrapper);
        if (flag) {
            version.setLogId(version.getLogId() + ":" + copyVersion.getLogId());
            return version;
        } else {
            return getLogVersion(queryWrapper);
        }
    }

    /***
     * 初始化一个版本
     * @param type
     * @param history
     * @param startLog
     * @return
     */
    private PLogVersion initPLog(Integer type, Integer history, String startLog, Integer maxLogId) {
        //初始化第一个
        PLogVersion v = new PLogVersion();
        v.setLogId(startLog);
        v.setVersion(1);
        v.setType(type);
        v.setIsHistoryVersion(history);
        v.setCreatedTime(new Date());
        v.setMaxLogId(maxLogId);
        this.save(v);
        return v;
    }

    /***
     * 获取人员最后log版本
     * @return
     */
    public synchronized PLogVersion getUserLogInfo() {
        LambdaQueryWrapper<PLogVersion> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PLogVersion::getType, 1)
                .eq(PLogVersion::getIsHistoryVersion, 1);
        PLogVersion version = this.getOne(queryWrapper);
        if (version == null) {
            return initPLog(1, 1, "200001", 516665);
        }
        if (Integer.valueOf(version.getLogId()) > (version.getMaxLogId() == null ? 516665 : version.getMaxLogId()) + 501) {
            log.error("===========用户同步完成===================");
            return null;
        }
        return getLogVersion(queryWrapper);
    }

    /**
     * 更新日志
     */
    public void updatePersonLog(Boolean continueFlag, Integer type) {
        LambdaQueryWrapper<PLogVersion> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PLogVersion::getType, type)
                .eq(PLogVersion::getIsHistoryVersion, 1);
        PLogVersion version = this.getOne(queryWrapper);
        version.setContinueFlag(continueFlag);

        //执行更新
        queryWrapper.eq(PLogVersion::getVersion, version.getVersion());
        queryWrapper.eq(PLogVersion::getId, version.getId());
        this.update(version, queryWrapper);
    }
}
